package com.smile.convert;

import com.smile.model.response.response.GetOrganizationByIdResponseDTO;
import com.smile.model.response.GetOrganizationByIdResponse;

import java.util.ArrayList;
import java.util.List;

public class OrganizationByIdConverter {

    /**
     * 将DTO对象转换为Response对象（树形结构）
     */
    public static GetOrganizationByIdResponse convertToResponse(GetOrganizationByIdResponseDTO dto) {
        if (dto == null) {
            return null;
        }

        GetOrganizationByIdResponse response = new GetOrganizationByIdResponse();

        // 基础字段复制
        response.setId(dto.getId());
        response.setOrganizationName(dto.getOrganizationName());
        response.setSort(dto.getSort());
        response.setChargeName(dto.getChargeName());
        response.setStatus(dto.getStatus());
        response.setParentId(dto.getParentId());

        // 递归转换子节点
        if (dto.getChildren() != null && !dto.getChildren().isEmpty()) {
            List<GetOrganizationByIdResponse> childrenResponses = new ArrayList<>();
            for (GetOrganizationByIdResponseDTO childDto : dto.getChildren()) {
                childrenResponses.add(convertToResponse(childDto));
            }
            response.setChildren(childrenResponses);
        }

        return response;
    }

    /**
     * 将DTO列表转换为Response列表（树形结构）
     */
    public static List<GetOrganizationByIdResponse> convertListToResponse(List<GetOrganizationByIdResponseDTO> dtoList) {
        if (dtoList == null) {
            return null;
        }

        List<GetOrganizationByIdResponse> responseList = new ArrayList<>();
        for (GetOrganizationByIdResponseDTO dto : dtoList) {
            responseList.add(convertToResponse(dto));
        }
        return responseList;
    }

}
